var express = require("express");
var router = express.Router();
let crypto = require("crypto");
let jwt = require("jsonwebtoken");
let leaveservice = require('../service/leaveservice')
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
  });

router.post('/add',(req,res,next)=>{
    let token = req.headers.token; //放在头部

    jwt.verify(token,'abc',async (err,info)=>{
        if(err){
            res.json({
                code:500,
                msg:"token解析异常",
            })
        }else{

            let usertype = info.usertype //获取类别
            if(usertype==2){
                let {reason,start,end,extend,type=1} = req.body
                let {userid,username} = info
                let result = await leaveservice.add({userid,username,reason,start,end,extend,type,})
                res.json({
                    code:200,
                    msg:'请假成功',
                    result
                })
            }else{
                res.json({
                    code:500,
                    msg:'权限不够'
                })
            }
        }
    })
    // userid:String,//用户编号
    // username:String,//用户姓名
    // reason:String,//请假理由
    // start:Date,//开始时间
    // end:Date,//结束时间
    // extend:String,//附件
    // type:Number,//状态 1：待审批 2 同意 3 拒绝
})//添加
//查询自己的
router.get('/studentlist',(req,res,next)=>{
    let token = req.headers.token; //放在头部
    if(token){
        jwt.verify(token,'abc',async (err,info)=>{
            if(err){
                res.json({
                    code:500,
                    msg:'token解析异常'
                })
            }else{
                let {userid,usertype} = info;
                if(usertype == 2){
                        let result= await leaveservice.query({userid})
                        res.json({
                            code:200,
                            msg:result
                        })
                }else{
                    res.json({
                        code:500,
                        msg:"权限不够"
                    })
                }
            }
        })
    }else{
        res.json({
            code:500,
            msg:"请登录，权限不够"
        })
    }
    




})
router.get('/allstudent',(req,res,next)=>{
    let token = req.headers.token;
    jwt.verify(token,'abc',async(err,info)=>{
        if(err){
            res.json({
                code:500,
                msg:'token解析异常'
            })
        }else{
           let {usertype} = info
           if(usertype == 1){
            let {pageindex,pagesize,type,key} = req.query
            
            let params = {}
            params.reason = new RegExp(key);//这样可以表示reason里面有这个key
            if(type){
                params.type = type
            }
            let result = await leaveservice.queryall(params,(pageindex-1)*pagesize,pagesize)
            let count = await leaveservice.count(params)

            res.json({
                count,
                code:200,
                msg:'成功',
                data:result,
                
            })
           }else{
            res.json({
                code:500,
                msg:'权限不够'
            })
           }
        }
    })
})
router.patch('/updatetype',(req,res,next)=>{ //老师处理请假
    let token = req.headers.token;
    jwt.verify(token,'abc',async(err,info)=>{
        if(err){
            res.json({
                code:500,
                msg:'token解析异常'
            })
        }else{
           let {usertype} = info
           let {_id,type} = req.body;

           if(usertype == 1){
            let result = await leaveservice.update({_id},{type});
            res.json({
                code:200,
                msg:'成功',
                data:result
            })
           }else{
            res.json({
                code:500,
                msg:'权限不够'
            })
           }
        }
    })
})
module.exports = router;
